<template>
	<view>
		<view style="background-color: #FFFFFF;">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-货位调整</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="sub" v-model="val" type="text" placeholder="输入关键词" focus></input>
			</view>
			<view class="action">
				<button class="cu-btn bg-gradual-green shadow-blur round" @click="clerInv">清空</button>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view class="cu-form-group margin-top" style="margin-top: 13%;">
			<radio :class="radio=='0'?'checked':''" :checked="radio=='0'" value="0" @click="aradio(0)">调整前</radio>
			<radio :class="radio=='1'?'checked':''" :checked="radio=='1'" value="1" @click="aradio(1)">调整后</radio>
		</view>
		<view>
			<view class="flex-sub text-center solid-bottom text-xl padding">
				<text class="text-black text-bold">货位信息</text>
			</view>
		</view>
		<view class="text-gray cu-form-group " style="color: red;">
			<text>调整前货位：{{hwname}}</text>
		</view>
		<view class="text-gray cu-form-group" style="color: #0081FF;">
			<text>调整后货位：{{hwname1}}</text>
		</view>
		<view>
			<view class="flex-sub text-center solid-bottom text-xl padding">
				<text class="text-black text-bold">产品信息</text>
			</view>
		</view>
		<view class="flex-sub solid-bottom text-xl" >
			<view class="text-gray cu-form-group">
				<text class="text-blue margin-right-xs">存货编码：{{viewCode}}</text> 
			</view >
			<view class="text-gray cu-form-group">
				<text class="text-blue margin-right-xs">存货名称：{{cinvname}}</text> 
			</view>
			<view class="text-gray cu-form-group">
				<text class="margin-right-xs" style="color: red;font-weight: 1000;">调整数量：</text>
				<input placeholder="0" name="input" type="number" v-model="qty"></input>
			</view>
		</view>
		<view class="action-section">
			<button type="primary" class="no-border confirm-btn" :disabled="ishow" @click="uploadData">调整</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				CustomBar: this.CustomBar, //滚动定位用
				val:'',//用于接收扫码的值
				radio:0,//单选的值
				hwcode:'',//货位编码
				hwname:"",//货位名称
				hwcode1:'',//货位编码
				hwname1:"",//货位名称
				qty:'',//调整数量
				ishow:false,//是否可用
				viewCode:'',//存货
				cinvcode:'',//存货
				cinvname:'',//存货
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onLoad() {
			// this.sub();
		},
		methods: {
			/** 扫发出声音*/
			makeSound(name){
				console.log("=====testClick=====");
				let src = '../../static/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			/**clerInv
			 *数据清空
			 */
			clerInv(){
				this.val='';//用于接收扫码的值
				this.radio=0;//单选的值
				this.hwcode='';//货位编码
				this.hwname="";//货位名称
				this.hwcode1='';//货位编码
				this.hwname1="";//货位名称
				this.qty='';//调整数量
				this.ishow=false;//是否可用
				this.viewCode='';//存货
				this.cinvcode='';//存货
				this.cinvname='';//存货
			},
			aradio(e){
				this.radio = e;
			},
			/**
			 * 使用于扫码事件发生
			 */
			sub(){
				if(this.val.indexOf("hw") > -1){
					uni.request({
						url:this.serverURL_a+'/so/App/getHw',
						data:{
							hw : this.val,
							__sid:this.userInfo.sessionid
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						},
						method: 'POST',
						success:function(data){
							this.val = '';
							var requrt = data.data;
							if(requrt.code == '1001'){
								var hwdata = requrt.resMap;
								if(this.radio == 0){
									this.hwcode = hwdata.cposcode;
									this.hwname = hwdata.cposname;
								}else{
									this.hwcode1 = hwdata.cposcode;
									this.hwname1 = hwdata.cposname;
								}
							}else{
								uni.showModal({
								    content: requrt.msg,
									showCancel:false,
								});
								return;
							}
						}.bind(this),
						fail:function(){
							this.$api.msg('服务已停止,请稍后再试');
							return;
						}.bind(this),
					});
					return;
				// }else if(this.val == '' || (this.val.indexOf("sc") == -1 && this.val.indexOf("dh") == -1 && this.val.indexOf("inv") == -1 && this.val.indexOf("po") == -1 && this.val.indexOf("611-") == -1)){
				}else if(this.val == ''){
					this.val = '';
					uni.showModal({
					    content: "请扫描正确的条码!",
						showCancel:false,
					});
					this.val = '';
					return;
				}
				uni.request({
					// url:this.serverURL_a+'/bas/basInventory/findMapCinvcode',
					url:this.serverURL_a+'/barcode/decode/getInvInfo',
					data:{
						// hw : this.val,
						__sid:this.userInfo.sessionid,
						barCode: this.val,
						bneedStore: '0',
						bmustAcct:'0'
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(res){
						console.log(res)
						this.val = '';
						if(res.data.result=="false"){
							uni.showModal({
							  content: res.data.message,
							  showCancel:false,
							})
							return false;
						}
						this.viewCode = res.data.data.basInv.viewCode;//存货
						this.cinvcode = res.data.data.basInv.cinvcode;//存货
						this.cinvname = res.data.data.basInv.cinvname;//存货 
						this.qty = 1;
					}.bind(this),
					fail: function() {
						uni.hideLoading()
						this.$api.msg('无法连接服务，请联系管理');
						this.isReC = false;
						return;
					}.bind(this)
				})
			},
			/**
			 * 扫码
			 */
			toScan(){
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						this.val = rdata;
						setTimeout(function(){
							this.sub();
						}.bind(this),100);
						
					}.bind(this)
				});
			},
			/**
			 * 数据上传
			 */
			uploadData(){
				this.ishow = true;
				if(this.cinvcode == '' || this.hwcode == ''){
					this.$api.msg("请检查数据，存货和货位信息不能为空!");
					this.ishow = false;
					this.makeSound("sb");
					return;
				}
				if(this.qty <= 0 ){
					this.$api.msg("请检查数据，调整数量等于和小于【零】，无意义!");
					this.ishow = false;
					this.makeSound("sb");
					return;
				}
				
				//提交数据
				uni.showModal({
					content: '是否提交\r\n当前调整数量：'+this.qty,
					success: function(res) {
						if(res.confirm){
							uni.showLoading({
							    title: '数据提交中',
								 mask:true
							});
							uni.request({
								url:this.serverURL_a + '/wh/App/posJustInv?__ajax=json',
								data:{
									cbposcode:this.hwcode,
									caposcode:this.hwcode1,
									cinvcode:this.cinvcode,
									iquantity:this.qty,
									__sid:this.userInfo.sessionid
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
								},
								method: 'POST',
								success:function(data){
									uni.hideLoading();
									this.ishow = false;
									var request = data.data;
									if(data.statusCode!="200"){
										uni.showModal({
										    content: request.message,
											showCancel:false,
										});
										this.makeSound("sb");
										return;
									}
									if(request.result == 'true'){
										this.$api.msg('提交成功!');
										this.viewCode = '';//存货
										this.cinvcode = '';//存货
										this.cinvname = '';//存货 
										this.qty = '';
										this.makeSound("cg");
										return;
									}else{
										uni.showModal({
											content: request.message,
											showCancel:false,
											success: function(res) {
												return
											},
										});
										this.makeSound("sb");
										return;
									}
								}.bind(this),
								fail:function() {
									this.$api.msg('无法连接服务，请联系管理');
									this.ishow = false;
									return;
								}.bind(this)
							})
						}else if (res.cancel) {
							this.ishow = false;
							return;
						}
					}.bind(this)
				});
			},
		}
	}
</script>

<style>
	/* 底部栏 */
	.action-section{
		/* #ifdef H5 */
		/* #endif */
		position:fixed;
		left: 75%;
		bottom:30upx;
		z-index: 95;
		display: flex;
		align-items: center;
		background: rgba(255,255,255,.9);
		border-radius: 40upx;
		.checkbox{
			height:52upx;
			position:relative;
			image{
				width: 52upx;
				height: 100%;
				position:relative;
				z-index: 5;
			}
		}
		.clear-btn{
			position:absolute;
			left: 26upx;
			top: 0;
			z-index: 4;
			width: 0;
			height: 52upx;
			line-height: 52upx;
			padding-left: 38upx;
			font-size: $font-base;
			color: #fff;
			background: $font-color-disabled;
			border-radius:0 50px 50px 0;
			opacity: 0;
			transition: .2s;
			&.show{
				opacity: 1;
				width: 120upx;
			}
		}
		.total-box{
			flex: 1;
			display:flex;
			flex-direction: column;
			text-align:right;
			padding-right: 40upx;
			.price{
				font-size: $font-lg;
				color: $font-color-dark;
			}
			.coupon{
				font-size: $font-sm;
				color: $font-color-light;
				text{
					color: $font-color-dark;
				}
			}
		}
		.confirm-btn{
			padding: 0 38upx;
			margin: 0;
			border-radius: 100px;
			height: 76upx;
			line-height: 76upx;
			font-size: $font-base + 2upx;
			background: $uni-color-primary;
		}
	}
</style>
